angular.module('meMoreListControllers', [])

  .controller('meMoreListCtrl', function ($scope, $state,$ionicActionSheet,$cordovaFileTransfer, $timeout,$ionicPopup,Storage, MoreList, Member, ENV,Toast, History) {

    $scope.ENV = ENV;
    //加载官方微信二维码图片
    //$scope.$on('$ionicView.beforeEnter', function () {
      MoreList.getMoreListPageInfo();
   // });

    //处理界面信息
    $scope.$on('Me.getMoreListPageInfo', function () {
      var result = MoreList.getMoreListPageInfoJson();
      $scope.au_code_url = result.au_code_url;
      //console.log($scope.au_code_url);
      $scope.au_weibo = result.au_weibo;
      $scope.au_weixin = result.au_weixin;

    });
    //长按保存图片
    $scope.downloadImg = function(){
        var timestamp=new Date().getTime();
        var url = ENV.picPath+$scope.au_code_url;
        var targetPath = cordova.file.externalRootDirectory +"winelife/winelife_me"+timestamp+".jpg";
        var trustHosts = true;
        var options = {};
      $ionicActionSheet.show({
        buttons: [
          {text: '保存到手机相册'}
        ],
        cancelText: '取消',
        buttonClicked: function (index) {
          if(index==0){
            $cordovaFileTransfer.download(url, targetPath, options, trustHosts)
              .then(function(result) {
                Toast.showMessage("保存成功");
              }, function(err) {
                Toast.showMessage("保存成功");
              }, function (progress) {
              });
          }
          return true;
        }
      });
    }

    //退出登录
    $scope.logout = function () {
      $ionicPopup.confirm({
        template: '<p style="text-align: center;font-size: 16px;">确定要退出登录吗？</p>',
        cancelText: '取消',
        okText: '确定',
        okType: 'hd-red-btn',
        cancelType: 'cancel-esc-btn'
      }).then(function (res) {
        if (res) {
          Member.logout();
          Member.setFirstInLogin();//不是第一次到登录界面
          $state.go('login');
        }
      });
    };

    $scope.gotoWeiBo = function () {
      var browser = cordova.ThemeableBrowser.open($scope.au_weibo, '_system', {
        statusbar: {
          color: '#ffffffff'
        },
        toolbar: {
          height: 44,
          color: '#970b34'
        },
        title: {
          color: '#ffffff',
          staticText: '官方微博',
          showPageTitle: true
        },
        backButton: {
          wwwImage: 'img/back-btn.png',
          wwwImagePressed: 'img/back-btn.png',
          wwwImageDensity: 2,
          align: 'left',
          event: 'backPressed'
        },
        backButtonCanClose: true
      });

      browser.addEventListener('backPressed', function (e) {
        browser.close();
      });
    }


  });
